package com.lwb.ztx.news.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lwb.ztx.R;
import com.lwb.ztx.api.Api;
import com.lwb.ztx.bean.NewsAndBannerBean;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.news.presenter.NewsListPresenter;
import com.lwb.ztx.news.presenter.NewsListPresenterImpl;
import com.lwb.ztx.news.view.NewsListView;
import com.lwb.ztx.news.widget.content.NewsContent;
import com.lwb.ztx.utils.ActivitySkipUtils;
import com.lwb.ztx.utils.LogUtils;
import com.recker.flybanner.FlyBanner;

import java.util.ArrayList;
import java.util.List;

/**
 * A fragment representing a list of Items.
 * <p/>
 * interface.
 */
public class NewsListFragment extends Fragment implements NewsListView,XRecyclerView.LoadingListener,FlyBanner.OnItemClickListener{

    private int mColumnCount = 1;

    public static final int NEWS_TYPE = 1;

    private int newsType = 1;
    private String newsUrl;

    private NewsListPresenter presenter;

    private NewsContent content;

    private NewsAdapter adapter;

    private XRecyclerView recyclerView;

    private Context mContext;

    private int currentPage = 1;

    private int ptrStyle = ProgressStyle.SysProgress;

    private FlyBanner banner;

    List<NewsBean> bannerBeans;

    private View rootView;

    private NewsAdapter.OnItemClickListener listener = new NewsAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(NewsBean bean) {
            readNews(bean);
        }
    };


    public NewsListFragment() {
        LogUtils.i("NewsListFragment create");
    }

    @SuppressLint("ValidFragment")
    public NewsListFragment(int type, String newsUrl) {
        newsType = type;
        this.newsUrl = newsUrl;
        LogUtils.i("NewsListFragment create type:" + type + ", newsUrl:" + (null == newsUrl));
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i("MYNEWS" + "NewsListFragment create " + newsUrl);
        if(savedInstanceState != null) {
            String url = savedInstanceState.getString("url");
            if (url != null && !url.equals("")) {
                newsUrl = url;
                LogUtils.i("oncreate: " + url);
            }
        }
        presenter = new NewsListPresenterImpl(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LogUtils.i("MYNEWS" + "NewsListFragment createview " + newsUrl);
        if(savedInstanceState != null) {
            String url = savedInstanceState.getString("url");
            if (url != null && !url.equals("")) {
                newsUrl = url;
                LogUtils.i("oncreate View: " + url);
            }
        }
        if (rootView == null) {
            mContext = this.getActivity();
            View view = inflater.inflate(R.layout.fragment_item_list, container, false);
            recyclerView = (XRecyclerView) view.findViewById(R.id.news_list);
            recyclerView.setLoadingListener(this);
            recyclerView.setRefreshProgressStyle(ptrStyle);
            recyclerView.setLoadingMoreProgressStyle(ptrStyle);
            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(mContext, mColumnCount));
            }
            content = new NewsContent();
            adapter = new NewsAdapter(mContext, content);
            adapter.setListener(listener);
            recyclerView.setAdapter(adapter);

            View bannerView = inflater.inflate(R.layout.news_banner_view, container, false);
            initBannerView(bannerView);
            recyclerView.addHeaderView(bannerView);
            refreshNews();
            rootView = view;
        }

        ViewGroup parentView = (ViewGroup) rootView.getParent();
        if (parentView != null) {
            parentView.removeView(rootView);
        }
        return rootView;

    }

    private void initBannerView(View bannerView) {
        banner = (FlyBanner) bannerView.findViewById(R.id.banner_view_pager);
        banner.setPoinstPosition(FlyBanner.RIGHT);
        banner.setOnItemClickListener(this);
    }


    private void refreshNews() {
        LogUtils.i("NewsListFragment refresh: " +  newsUrl);
        if (newsType > 0) {
            presenter.getNews(Api.getUrl(newsUrl,1), 1);
        }
    }

    @Override
    public void onAttach(Context context) {
        LogUtils.i("MYNEWS" + "NewsListFragment attach " + newsUrl);
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void addNews(NewsAndBannerBean beans) {
        LogUtils.d(beans.toString());
        bannerBeans = beans.getBannerBeans();
        if(bannerBeans.size() > 0) {
            List<String> images = new ArrayList<>();
            for(int i=0;i<bannerBeans.size();i++) {
                String image = bannerBeans.get(i).getThumbnail();
                if(null == image || image.equals("") ) {
                    continue;
                }
                images.add(image);
            }
            LogUtils.i("BANNER LIST: " + images.toString());
            if (images.size() != 0 ) {
                banner.setImagesUrl(images);
                banner.setVisibility(View.VISIBLE);
            } else {
                banner.setVisibility(View.GONE);
            }
        } else {
            banner.setVisibility(View.GONE);
        }

        recyclerView.refreshComplete();
        if(beans.getBeans().size() == 0) return;
        currentPage = 1;
        content.refreshNews(beans.getBeans());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void addMoreNews(NewsAndBannerBean beans) {
        recyclerView.loadMoreComplete();
        if(beans.getBeans().size() == 0) return;
        content.addNews(beans.getBeans());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        refreshNews();
    }

    @Override
    public void onLoadMore() {
        currentPage ++;
        presenter.getNews(Api.getUrl(newsUrl,currentPage), currentPage);
    }

    @Override
    public void onItemClick(int position) {
        if(bannerBeans.size() > position) {
            NewsBean bean = bannerBeans.get(position);
            readNews(bean);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("url", newsUrl);
        LogUtils.i("MYNEWS" + "NewsListFragment save state " + newsUrl);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.i("MYNEWS" + "NewsListFragment destory "+ newsUrl);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.i("MYNEWS" + "NewsListFragment destoryView "+ newsUrl);
    }

    private void readNews(NewsBean bean) {
        ActivitySkipUtils.readNews(mContext,bean);
    }
}
